//
//  YHHomepageVC.m
//  LyhToolsTest
//
//  Created by LYH on 2018/10/26.
//  Copyright © 2018年 LYH.com. All rights reserved.
//

#import "YHHomepageVC.h"
#import "YHcollectionVC.h"     //collection使用
#import "YHDetailLeftCell.h"
#import "YHUMshareAlertView.h" //友盟弹窗view
#import "YHSyntheticImageVC.h" //合成图片vc
#import "YHViewTestVC.h"       //
#import "YHRuntimeVC.h"        //Runtime练习
#import "YHRunloopVC.h"        //Runloop练习
#import "YHHomeListModel.h"    //列表model
#import "YHObserverPracticeVC.h" //观察者模式

#import "YHFMDBViewC.h" //FMDB使用
#import "LyhToolsTest-Swift.h"//工程名 + -Swift.h//oc调用swift文件必需添加
@interface YHHomepageVC ()<UITableViewDelegate,UITableViewDataSource,YHUMshareviewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) YHUMshareAlertView *shareView;

@property (nonatomic, strong) NSArray *Titles;

@end

@implementation YHHomepageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initnava];
    [self tableView];

    //plist
    
//    [self readJsondata];
    
}
- (void)initnava
{
    WeakSelf;
    wself.ts_navgationBar = [TSNavigationBar navWithTitle:@"首页" rightTitle:@"分享" rightAction:^{
        NSArray *images = @[];
        self.shareView = [[YHUMshareAlertView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeigth) ImageArry:images];
        self.shareView.delegate = self;
        [self.shareView ShowAlterview];
    }];
    
}
#pragma -mark ————————————————————YHUMshareviewDelegate——————————————————
- (void)didSelectItemAtIndex:(NSInteger)index
{
    [_shareView  disAlterview];
}
#pragma -mark -----------------------------tableview datasource and delegate---------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.Titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YHDetailLeftCell *cell = [YHDetailLeftCell cell:tableView];
    
    cell.TitleLab.text = self.Titles[indexPath.row];
  
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:
        {
            YHcollectionVC *vc = [[YHcollectionVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        } break;
        case 1:
        {
            YHSyntheticImageVC  *vc = [[YHSyntheticImageVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 3:
        {
            YHViewTestVC *vc = [[YHViewTestVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 4:
        {
            YHSwiftViewController *vc = [[YHSwiftViewController alloc]init];
            vc.Arry0 = @[@"1",@"2",@"3"];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
       case 5:
        {
            YHRuntimeVC *vc = [[YHRuntimeVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 6:
        {
            YHRunloopVC *vc = [[YHRunloopVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 7:
        {
            YHObserverPracticeVC *vc = [[YHObserverPracticeVC alloc]init];
             [self.navigationController pushViewController:vc animated:YES];
        }break;
        case 8:
        {
        /*要达到跳转到我的界面的下一级，而且返回回来是我的界面
         *先跳转到我的界面self.tabBarController.selectedIndex = 3;
         *dispatch_after 延迟操作为啦先加载接收通知的方法，然后才可以响应通知操作
         */
        self.tabBarController.selectedIndex = 3;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                 [[NSNotificationCenter defaultCenter] postNotificationName:@"wofatongzhi" object:self userInfo:nil];
            });
        }break;
        case 9:
        {
            YHFMDBViewC *vc = [[YHFMDBViewC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }break;
        default:
           
            break;
    }
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1.0f;
}
#pragma -mark ————————————————————Lay————————————————————
//懒加载tableview
- (UITableView *)tableView{
    if(!_tableView){
        _tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, kScreenWidth, kScreenHeigth-SafeAreaTopHeight-SafeAreaTabBarHeight) style:UITableViewStyleGrouped];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.estimatedSectionHeaderHeight = 0;
            _tableView.estimatedSectionFooterHeight = 0;
        }
        _tableView.backgroundColor=[UIColor whiteColor];
        //竖直滚动条不显示
        _tableView.showsVerticalScrollIndicator=NO;
        //iOS 8开始的自适应高度，可以不需要实现定义高度的方法
        //_tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 0;
        _tableView.rowHeight=50;
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
        //设置分割线铺满cell
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
- (NSArray *)Titles
{
    if (!_Titles) {
        _Titles = @[@"collectionView用法",@"图片文字合成",@"淘宝详情",@"view的分类",@"OC-Swift混编",@"Runtime的使用",@"Runloop体验",@"观察者模式",@"通知的使用",@"数据库FMDB使用"];
    }
    return _Titles;
}
//读取json 数据 转成新的json
- (void)readJsondata
{
      // 获取文件路径
    NSString *path = [[NSBundle mainBundle]pathForResource:@"ylb_address" ofType:@"json"];
    // 将文件数据化
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
   //对数据进行JSON格式化并返回字典形式
    
    NSDictionary *dicdata = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSArray *arrydata = dicdata[@"RECORDS"];
    
    NSMutableArray *resultArry = [[NSMutableArray alloc]init];
    
    
    NSMutableArray *shenArry = [[NSMutableArray alloc]init];
    NSMutableArray *cityArry = [[NSMutableArray alloc]init];
    NSMutableArray *quArry = [[NSMutableArray alloc]init];
    
    for (NSDictionary *dic in arrydata) {
        NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithDictionary:dic];
        if ([mudic [@"ad_level"] intValue]==1) {//省
            NSMutableArray *arry = [[NSMutableArray alloc]init];
            [mudic setValue:arry forKey:@"cityList"];
            
            [shenArry addObject:mudic];
        }
        //市
        if([mudic [@"ad_level"] intValue]==2){
             NSMutableArray *arry = [[NSMutableArray alloc]init];
            [mudic setValue:arry forKey:@"areaList"];
            [cityArry addObject:mudic];
        }
        //区
        if([mudic [@"ad_level"] intValue]==3){
            [quArry addObject:mudic];
        }
        
    }
//    for (int i=0; i<quArry.count; i++) {
//
//
//        for (int j=0; j<cityArry.count; j++) {
//
//
//
//            for (int k=0; k<shenArry.count; k++) {
//
//
//
//            }
//
//
//        }
//
//
//    }
    
    NSMutableArray *Newcity = [[NSMutableArray alloc]init];
    //区
    for (NSDictionary *citdic in cityArry) {
        for (NSDictionary *qudic in quArry) {
            if ([qudic[@"parent_id"] intValue] == [citdic[@"id"] intValue]) {
                
                [citdic[@"areaList"] addObject:qudic];
                
            }
        }
        [Newcity addObject:citdic];
    }
    
   
    
  
    
    for (NSDictionary *shendic in shenArry) {
        
        for (NSDictionary *citdic in Newcity) {
            
            if ([citdic [@"parent_id"] intValue] == [shendic[@"id"] intValue]) {
                [shendic[@"cityList"] addObject:citdic];
                
            }
        }
       [resultArry addObject:shendic];
    }
    
   
    
   
   
    
    
    TSLog(resultArry);

   
    [resultArry writeToFile:@"/Users/hong-mac/Desktop/jsonwenjian/JPaddress.json" atomically:YES];
    
//    NSArray *Plistpath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//
//    NSString *documentsPath = [Plistpath objectAtIndex:0];
//    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"JPaddress.plist"];
//
//    //写入文件
//    [resultArry writeToFile:plistPath atomically:YES];
    
    
    
}

@end
